<?php
/**
 * Created by PhpStorm.
 * User: jswei
 * Date: 2018/4/24
 * Time: 8:54
 */

namespace app\admin\model;

use app\first\model\Notice;
use think\Db;

class ShopGoodsOrder extends Base {
    /**
     * @param array $where
     * @param string $order
     * @param int $limit
     * @param string $field
     * @param bool $out
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($where = [], $order = '', $limit = 10, $field = '', $out = false){
//        p($where);
//        p($field);die;
        $list = $this
            ->field($field,$out)
            ->with(['member','shop'])
            ->where($where)
            ->order($order)
            ->paginate($limit);
        $pic = new Pic();
        $page = $list->currentPage();
        $total = $list->total();
        foreach ($list as $k => &$v){
            $v['sort_asc'] = $k + 1 + ($page-1) * $limit;
            $v['sort_desc'] = $total - ($page-1) * $limit - $k;
        }
        return $list;
    }

    /**
     * 审核
     * @param $id
     * @param int $type
     * @param int $aid
     * @param string $info
     * @param null $out
     * @return bool
     */
    public function send($id,$type=1,$aid=0,$info='',&$out=null){
        $_info = $this->find($id);
        if(!$_info){
            $out = '订单不存在';
            return false;
        }
        if($_info['status']!=2){
            $out = '订单处理了';
            return false;
        }
        if($type==2){
            if(empty($info)){
                $out = '请输入拒绝理由';
                return false;
            }
            $title= "出库失败";
            $_info->info = $info;
            $_status = 8;
            $content = "抱歉！您购买的物品出库失败，原因：{$info}";
        }else{
            $_status = 3;
            $title= "出库成功";
            $content = "恭喜您！您购买的物品出库成功！";
        }
        $_info->status = $_status;
        Db::startTrans();
        $result = $_info->save();
        $result2 = (new Notice())->addNew($_info['uid'],$title,$content,6,'',$_info['id']);
        if($result && $result2) {
            $_data = $_info->toArray();
            $_data['sql'] = $_info->getLastSql();
            $_data['msg']=$content;
            $out = $_data;
            Db::commit();
            return true;
        }else{
            $out = '操作失败';
            Db::rollback();
            return false;
        }
    }

    /**
     * @param int $id
     * @param string $field
     * @return array|\PDOStatement|string|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getOneById($id = 0, $field = '*'){
        $info = $this
            ->field('update_time',true)
            ->with(['member','shop'])
            ->where('id','eq',$id)
            ->find();
        return $info;
    }

    public function add($data){
        $this->save($data);
    }

    public function member(){
        return $this->hasOne('Member','id','uid')
            ->bind([
                'account',
                'nickname'
            ]);
    }

    public function shop(){
        return $this->hasOne('Shop','id','shop_id')
            ->bind([
                'shop_name'=>'title',
                'real_name',
                'contact',
                'service'
            ]);
    }

    /**
     * @param $value
     * @return string
     */
    public function setContentAttr($value){
        if($value){
            return htmlspecialchars($value);
        }else{
            return '';
        }
    }

    /**
     * @param $value
     * @return string
     */
    public function getContentAttr($value){
        if($value){
            return htmlspecialchars_decode($value);
        }else{
            return '';
        }
    }
}